package com.irdstudio.biz.rule.common;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BizUtil {

    public static String getUUID() {
        return getUUID(null);
    }

    public static String getUUID(String prefix) {
        String uuid =  UUID.randomUUID().toString();
        uuid = prefix != null ? prefix + uuid : uuid;
        return DigestUtils.md5Hex(uuid);
    }

    public static String getCurrentTime() {
        return DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Camel-Case 驼峰命名正则表达式
     */
    private static final String CAMEL_CASE_REGAX = "\\B([A-Z])" ;

    private static final String HYPHEN_REGAX = "\\B(_[a-z])" ;
    /**
     * 判断字段名是否为驼峰命名
     *
     * @param property
     * @return
     */
    private static boolean isCamelCaseProperty(String property){
        Pattern pattern = Pattern.compile(CAMEL_CASE_REGAX) ;
        Matcher matcher = pattern.matcher(property) ;
        if(matcher.find()){
            return true ;
        }else{
            return false ;
        }
    }

    /**
     * 将下划线命名转换成驼峰命名
     * c_pk_id => pkId
     *
     * @param name
     */
    public static String getCamelCaseNamed(String name){
        if (name == null || name.length() <= 2 || isCamelCaseProperty(name)) {
            return name;
        }
        String camel = name ;

        Pattern pattern = Pattern.compile(HYPHEN_REGAX);
        Matcher matcher = pattern.matcher(camel);
        String r = camel ;
        while(matcher.find()){
            String m = matcher.group(1) ;
            r = r.replaceFirst(m, String.valueOf(m.charAt(1)).toUpperCase());
        }
        return r ;
    }

    /**
     * hyphen-delimited string
     */
    public static String transferCamelCaseToHyphen(String camel){
        Pattern pattern = Pattern.compile(CAMEL_CASE_REGAX) ;
        Matcher matcher = pattern.matcher(camel);
        String r = camel ;
        while(matcher.find()){
            String m = matcher.group(1) ;
            r = r.replaceFirst(m, "_"+m.toLowerCase());
        }
        return r ;
    }


    /**
     *
     * 将字符用单引号包裹，并用分隔符拼接
     *
     * @param list
     * @param separator
     * @return
     */
    public static String inSql(List<String> list,String separator ){
        String sql = "'%s'";
        if(CollectionUtils.isEmpty(list))
            return null;
        if(StringUtils.isBlank(separator))
            separator = "";
        int arraySize = list.size();
        int buffSize =
                (arraySize == 0)? 0 :
                        arraySize * ((list.get(0)==null?16:list.get(0).length() + separator.length())) ;
        StringBuffer sqlStr = new StringBuffer(buffSize);
        for(int i=0;i<arraySize;i++){
            if(i>0)
                sqlStr.append(separator);
            if(list.get(i) != null){
                String s = String.format(sql,list.get(i));
                sqlStr.append(s);
            }
        }
        return sqlStr.toString();
    }
    /**
     *
     * 将字符用单引号包裹，并用分隔符拼接 ,将Object[]转成List<String>
     *
     * @param list
     * @param separator
     * @return
     */
    public static String inSql(Object[] list, String separator){
        if(list != null){
            List<String> array = new ArrayList<>(list.length);
            for(int i=0;i<list.length;i++){
                array.add(list[i].toString());
            }
            return inSql(array,separator);
        }else return null;
    }
}
